package com.bignerdranch.android.geoquiz

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.nfc.Tag
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.lifecycle.ViewModelProviders

const val EXTRA_ANSWER_SHOWN="com.bignerdranch.android.geoquiz.answer_shown"
private const val EXTRA_ANSWER_IS_TRUE=
    "com.bignerdranch.android.geoquiz.answer_is_true"




class CheatActivity : AppCompatActivity()
{

    private  var mIsShowAnswer:Boolean =false
    private val  First_BUG :String="First Bug"
    private lateinit var api_text_view:TextView
    private lateinit var answerTextView: TextView
    private lateinit var showAnswerButton: Button
    private var answerIsTrue:Boolean=false
    private lateinit var cheat_text_view:TextView

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_cheat)
        answerIsTrue=intent.getBooleanExtra(EXTRA_ANSWER_IS_TRUE,false)
        answerTextView=findViewById(R.id.answer_text_view)
        showAnswerButton=findViewById(R.id.show_answer_button)
        api_text_view=findViewById(R.id.api_text_view)
        cheat_text_view=findViewById(R.id.cheat_number_cheat)

        if (savedInstanceState!=null)
        {
            mIsShowAnswer=savedInstanceState.getBoolean(First_BUG,false)
            if (answerIsTrue)
                {
                    setAnswerShownResult(true)
                }
            else{
                    setAnswerShownResult(false)
                }

        }



        showAnswerButton.setOnClickListener {

            val answerText=when{
                answerIsTrue->R.string.true_button
                else->R.string.false_button
            }
            answerTextView.setText(answerText)
            setAnswerShownResult(true)

        }


    }

    override fun onStart() {
        super.onStart()
        api_text_view.setText("API Level\u3000"+(Build.VERSION.SDK_INT).toString())

    }


    private fun onSaveInsranceState(savedInstanceState: Bundle)
    {
        super.onSaveInstanceState(savedInstanceState)
        savedInstanceState.putBoolean(First_BUG, mIsShowAnswer)
    }

    private fun setAnswerShownResult(isAnswerShown:Boolean)
    {
        val data=Intent().apply {
            putExtra(EXTRA_ANSWER_SHOWN,isAnswerShown)
        }
        setResult(Activity.RESULT_OK,data)
    }

    companion object{
        fun newIntent(packageContext:Context,answerIsTrue:Boolean):Intent{
            return Intent(packageContext,CheatActivity::class.java).apply {
                putExtra(EXTRA_ANSWER_IS_TRUE,answerIsTrue)
            }
        }

    }

}